<?php

namespace App\Http\Middleware;

use Closure;

class ResponseStatus
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \Closure $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $response = $next($request);
        return $response;
        if(env('APP_ENV') !='local')
        {
            $response = isset($response->original['error_code']) ? $this->noResponse($response) : $this->isResponse($response);
        }
        return $response;
    }

    public function noResponse($response)
    {
        $response->original['error_code'] = (int)$response->original['error_code'];

        return $response;
    }

    public function isResponse($response)
    {
        $response = isset($response->original) ? $this->originalCode($response) : $this->noOriginalCode($response);

        return $response;


    }

    public function originalCode($response)
    {
        $response->original['code'] = (int)$response->original['code'];

        return $response;
    }

    public function noOriginalCode($response)
    {
        foreach (collect($response) as $k => &$v) {
            $code         = json_decode($v, true);
            $code['code'] = (int)$code['code'];

            return $code;
        }
    }
}
